﻿using System.Linq;

namespace System.Web.Mvc
{
	public static partial class Holovacs
	{
		/// <summary>
		/// Determines if the logged-in user has one of the specified comma-delimited roles assigned.
		/// </summary>
		/// <param name="htmlHelper"></param>
		/// <param name="roles">A list of comma-separated role names.</param>
		/// <returns></returns>
		public static bool HasRole(this HtmlHelper htmlHelper, string roles)
		{
			var context = htmlHelper.ViewContext.HttpContext;
			if (!context.User.Identity.IsAuthenticated) return false;
			var user = context.User;
			foreach (var role in roles.Split(',').Select(r => r.Trim()))
			{
				if (user.IsInRole(role)) return true;
			}
			return false;
		}
	}
}
